﻿var from = (function () {
    function cinit(f) {
        initheader(f);
        initbody(f);
        initfooter(f);
        initfrom(f);
    }
    function initheader(f) {
        $('#from-title').html(f.title);
    }
    function initbody(f) {
        var tab = f.applyTo + "-tabs";
        $("#" + tab).remove();
        $("#" + f.applyTo).append('<div id="' + tab + '" class="easyui-tabs"></div>');
        $("#" + tab).tabs({
            fit: true,
            tabPosition: 'bottom'
        });
        var cm = f.parent.ds.modeldata.childmodel;
        var m;
        var sh = f.parent.fromconfig.grid;
        for (var i in cm) {
            m = cm[i];
            if (!m.visible) continue;
            if (sh[i] === false) continue;
            var tn = f.applyTo + "-table-" + i;
            var table = '<table id="' + tn + '" ></table>';
            $("#" + tab).tabs("add", {
                title: m.title,
                content: '<div id="' + tn + '-div">' + table + '</div>',
                selected: false
            });
            $("#" + tn + "-div").panel({ fit: true });
            var grid = new datagrid();
            grid.datasource = f.parent.ds.getcmsource(i);
            grid.datasource.page = 1;
            grid.applyTo = tn;
            grid.parent = f;
            grid.fit = true;
            grid.tabindex = i;
            grid.onDblClickRow = function (g, rowIndex, rowData) {
                if (g.tabindex == f.editindex) {
                    var tab = f.applyTo + "-tabs";
                    $("#" + tab).tabs("select", "作业");
                    for (var n in rowData) {
                        var box = f.findbox(n);
                        if (box == null) continue;
                        var c = g.getcol(n);
                        var v = rowData[n];
                        if (c == null)
                            box.setText(v);
                        else {
                            box.setValue(v);
                            if (c.formatter) {
                                box.setText(c.formatter(v, rowData, rowIndex));
                            }
                        }
                    }
                    f.editobj = rowData;
                }
            }
            f.griditems[i] = grid;
            grid.init();
        }
        $("#" + tab).tabs({
            onSelect: function (title, index) {
                if (title == "作业") return;
                f.loadchild(index);
            }
        });
    }
    function initfooter(f) {

    }
    function initfrom(f) {
        f.editindex = f.parent.fromconfig.editindex;
        var grid = f.griditems[f.editindex];
        if (f.edittitle == "")
            f.edittitle = grid.gettitle();
        var tab = f.applyTo + "-tabs";
        var fn = f.applyTo + "-table-from-div";
        $("#" + tab).tabs("add", {
            title: "作业",
            content: '<div id="' + fn + '" class="easyui-navpanel"><header><div class="m-toolbar"><span class="m-title">' + f.edittitle + '</span></div>'
            + '<div class="m-left"><a href="javascript:void(0)" iconCls="icon-clear" class="easyui-linkbutton">清空</a></div><div class="m-right"><a href="javascript:void(0)" iconCls="icon-save" class="easyui-linkbutton">保存</a></div></div></header>',
            selected: false
        });
        $("#" + fn).navpanel({ fit: true });
        $("#" + fn).append('<ul class="m-list"></ul>');
        var disablebox = f.parent.fromconfig.disablebox;
        for (var i in grid.items) {
            var mm = grid.datasource.getitem(grid.items[i].field);
            if (mm.field == "ROWINDEX" || mm.field == "CREATEDBY" || mm.field == "CREATIONDATE") continue;
            if (mm.visible) {
                var b = new textbox();
                b.field = mm.field;
                b.title = mm.title;
                b.box.disabled = mm.disabled;
//                if (disablebox == "*")
//                    b.box.disabled = true;
                b.box.readonly = mm.required;
                b.parent = f;
                b.applyTo = fn + " .m-list";
                b.init();
                f.boxs.push(b);
            }
        }
    }
    function getval(f) {
        for (var n in f.boxs) {
            var box = f.boxs[n];
            if (!box.box.disabled && !box.box.readonly)
                f.editobj[n] = box.getValue();
        }
        return f.editobj;
    }
    return {
        applyTo: "from",
        parent: null,
        ds: null,
        title: "",
        row: null,
        griditems: [],
        editindex: 0,
        boxs: [],
        edittitle: "",
        init: function () {
            cinit(this);
            $.mobile.go("#" + this.applyTo);
        },
        loadchild: function (index, w) {
            var f = this;
            f.ds.loadforeign(index, f.row, w, function (data) {
                var grid = f.griditems[index];
                grid.load(data);
            });
        },
        findbox: function (field) {
            var box = null;
            var name = field.toUpperCase();
            for (var i in this.boxs) {
                var e = this.boxs[i];
                if (e.field == name) {
                    box = e;
                    break;
                }
            }
            return box;
        }
    }
})